﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    //管理员工合同
    public class ContractController : Controller
    {
        private IEmployeeRepository employeeRepository;
        private IUnitOfWork unitOfWork;
        public ContractController(IEmployeeRepository employeeRepository,
            IUnitOfWork unitOfWork)
        {
            this.employeeRepository = employeeRepository;
            this.unitOfWork = unitOfWork;
        }
        //将使用期Employee转换为ContractModel,显示到Telrki的Grid当中,用于查看那些员工需要签订合同
        public IList<ContractModel> EmployeeToContractModel()
        {
            var employees = employeeRepository.GetAll().ToList();
            IList<ContractModel> contractModels = new List<ContractModel>();
            foreach (Employee e in employees)
            {
                contractModels.Add(new ContractModel()
                                       {
                                           ID = e.ID,
                                           Name = e.Name,
                                           Sex = e.Sex,
                                           EmployeeStatus = e.EmployeeStatus,
                                           BeginContractDate = e.BeginContractDate,
                                           EndContractDate = e.EndContractDate
                                       });
            }
            return contractModels;
        }
        //初始页面
        public PartialViewResult Index()
        {
            return PartialView();
        }
        //用于分页排序和筛选
        [GridAction]
        public ActionResult TelerikAjax()
        {
            return View(new GridModel(EmployeeToContractModel()));
        }
        //编辑合同页面Get页面
        [HttpGet]
        public PartialViewResult Edit(int id)
        {
            Employee employee = employeeRepository.GetById(id);
            ContractModel contractModel = new ContractModel();
            if (employee != null)
            {
                contractModel.ID = employee.ID;
                contractModel.Name = employee.Name;
                contractModel.Sex = employee.Sex;
                contractModel.EmployeeStatus = employee.Status;
                contractModel.ContractType = employee.ContractType;
                contractModel.BeginContractDate = employee.BeginContractDate;
                contractModel.EndContractDate = employee.EndContractDate;
                contractModel.ContractRemark = employee.ContractRemark;
            }
            return PartialView(contractModel);
        }
        //编辑合同页面Get页面
        [HttpPost]
        public RedirectToRouteResult Edit(ContractModel contractModel)
        {
            Employee employee = employeeRepository.GetById(contractModel.ID);
            //视图模型通过验证,对数据进行更新
            if (ModelState.IsValid)
            {
                employee.ContractType = contractModel.ContractType;
                employee.BeginContractDate = contractModel.BeginContractDate;
                employee.EndContractDate = contractModel.EndContractDate;
                employee.ContractRemark = contractModel.ContractRemark;
                employeeRepository.Update(employee);
                unitOfWork.Commit();
            }

            //重定向到"Index","HumanResources",仍然会加载Edit,因为session保存的原因
            return RedirectToAction("Index", "HumanResources");
        }
    }
}
